
package com.fx.apartment.modules.file.service.impl;


import com.fx.apartment.common.pops.QinUploadProperties;
import com.fx.apartment.common.tool.utils.FileUtil;
import com.fx.apartment.modules.file.entity.QinFile;
import com.fx.apartment.modules.file.service.IFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


/**
 * 文件上传 服务实现类
 *
 * @author QinXueFeng
 * @since 2021-04-19
 */
@Service
@AllArgsConstructor
public class FileServiceImpl implements IFileService {

    private final QinUploadProperties bladeUploadProperties;

    @Override
    public QinFile putFile(@RequestParam MultipartFile file) {
        QinFile qinFile = new QinFile();
        String url = bladeUploadProperties.getDomain();
        if(!url.endsWith("/")){
            url=url+"/";
        };
        String savePath = bladeUploadProperties.getSavePath();
        if(!savePath.endsWith("/")){
            savePath=savePath+"/";
        };
        qinFile.setLink(url+file.getOriginalFilename());
        qinFile.setName(file.getOriginalFilename());
        qinFile.setOriginalName(savePath+"/"+file.getOriginalFilename());
        FileUtil.toFile(file,new File(savePath +"/"+file.getOriginalFilename()));
        return qinFile;
    }

    @Override
    public QinFile putFile(MultipartFile file, String dir) {
        QinFile qinFile = new QinFile();
        String url = bladeUploadProperties.getDomain();
        if(!bladeUploadProperties.getDomain().endsWith("/")){
            url=url+"/";
        };
        String savePath = bladeUploadProperties.getSavePath();
        if(!savePath.endsWith("/")){
            savePath=savePath+"/";
        };
        qinFile.setLink(url+dir+"/"+file.getOriginalFilename());
        qinFile.setName(file.getOriginalFilename());
        qinFile.setOriginalName(savePath+"/"+file.getOriginalFilename());
        FileUtil.toFile(file,new File(savePath+file.getOriginalFilename()));
        return qinFile;
    }

}
